#include "gkt_rtc.h"
#include "window_common.h"
#include "string.h"

#if (GKT_USER_GUEST_MAXNUMS > 0)

#define	HOURMAX	GKT_DT_DAY_HOURS
#define MINMAX	GKT_DT_HOUR_MINUTES

enum{
	GUEST_UNLOCK_HANDLE_RESULT_SUCCESS = 0,
	GUEST_UNLOCK_HANDLE_RESULT_FAIL,
	GUEST_UNLOCK_HANDLE_RESULT_COMPLETE,
	GUEST_UNLOCK_HANDLE_RESULT_MAX
};

enum {
	GUEST_UNLOCK_STATE_NORMAL,
	GUEST_UNLOCK_STATE_DIALOG_RETURN,
	GUEST_UNLOCK_STATE_MAX
};

enum {
	WIDGET_ID_TITLE = 0,

	WIDGET_ID_START,
	WIDGET_ID_HOUR_S,
	WIDGET_ID_SEPARATOR1,
	WIDGET_ID_MIN_S,

	WIDGET_ID_END,
	WIDGET_ID_HOUR_E,
	WIDGET_ID_SEPARATOR2,
	WIDGET_ID_MIN_E,

	WIDGET_ID_OK_CANCEL,
	WIDGET_ID_MAX
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(guest_unlock_time_setting) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, WINDOW_WIDGET_DEFAULT_WIDTH, 16,
		WINDOW_TEXT_ID_guest_unlock_time),

	[WIDGET_ID_START] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_LARGE_SIZE, 
		WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_FONT_NORMAL_SIZE * 2 + 8,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_start_time),
	[WIDGET_ID_HOUR_S] = GKT_WIDGET_INPUTBOX_TEXT_DESC(
		GKT_WIDGET_F_FOCUS | GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 2 + 8,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_FONT_NORMAL_SIZE + 2 + 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		2, 2),
	[WIDGET_ID_SEPARATOR1] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 3 + 12, 
		WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_FONT_NORMAL_SIZE - 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		":"),
	[WIDGET_ID_MIN_S] = GKT_WIDGET_INPUTBOX_TEXT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 4 + 10, 
		WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_FONT_NORMAL_SIZE + 2 + 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		1, 2),

	[WIDGET_ID_END] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_LARGE_SIZE,
		WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		WINDOW_FONT_NORMAL_SIZE * 2 + 8,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_end_time),
	[WIDGET_ID_HOUR_E] = GKT_WIDGET_INPUTBOX_TEXT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 2 + 8, 
		WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		WINDOW_FONT_NORMAL_SIZE + 2 + 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		2, 2
	),
	[WIDGET_ID_SEPARATOR2] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 3 + 12, 
		WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		WINDOW_FONT_NORMAL_SIZE - 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		":"),	
	[WIDGET_ID_MIN_E] = GKT_WIDGET_INPUTBOX_TEXT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_FONT_LARGE_SIZE + WINDOW_FONT_NORMAL_SIZE * 4 + 10, 
		WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		WINDOW_FONT_NORMAL_SIZE + 2 + 2,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		2, 2),

	[WIDGET_ID_OK_CANCEL] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, 
		WINDOW_WIDGET_NORMAL_HEIGHT * 3,
		WINDOW_WIDGET_DEFAULT_WIDTH,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok),
};


gkt_time_u u_guest_unlock_time[2];

struct _guest_unlock_time_dialog {
	uint32_t	state;
};

struct _guest_unlock_time_dialog s_guest_unlock_time_dialog = {
	.state = GUEST_UNLOCK_STATE_NORMAL,
};

static uint32_t window_guest_unlock_time_setting_to_dec(gkt_widget_inputbox_data_s *textbox_data)
{
	uint32_t temp_data=0;
	int i=0;
	gkt_widget_inputbox_data_s *p;
	p=textbox_data;
	while(i<(p->size)){
		temp_data *= 10;
		temp_data += (p->data[i]-'0');
		i++;
	}
	return temp_data;
}

static void window_guest_unlock_time_setting_dec_to_char(uint32_t dec,
				gkt_widget_inputbox_data_s *textbox_data)
{
	textbox_data->data[0] = (dec / 10) + '0';
	textbox_data->data[1] = (dec % 10) + '0';
	textbox_data->size = 2;
	textbox_data->focus_index = 0;
}
//DATE FUNCTION
static int window_guest_unlock_time_data_handle(gkt_widget_id_t widget_id,
														gkt_time_u *input_time,uint32_t num,
															gkt_widget_inputbox_data_s *event_data)
{
	uint8_t retval = GUEST_UNLOCK_HANDLE_RESULT_SUCCESS;
	if(widget_id == WIDGET_ID_HOUR_S){
		if(num < HOURMAX){
			input_time[0].hour = num;
			gkt_widget_set_focus_by_id(WIDGET_ID_MIN_S);
		}
		else{
			event_data->focus_index = 0;
			gkt_widget_set_data_by_id(WIDGET_ID_HOUR_S,(uint32_t)event_data);
			retval = GUEST_UNLOCK_HANDLE_RESULT_FAIL;
		}
	}
	else if(widget_id == WIDGET_ID_MIN_S){
		if(num < MINMAX){
			input_time[0].minute = num;
			gkt_widget_set_focus_by_id(WIDGET_ID_HOUR_E);
		}
		else{
			event_data->focus_index = 0;
			gkt_widget_set_data_by_id(WIDGET_ID_MIN_S,(uint32_t)event_data);
			retval = GUEST_UNLOCK_HANDLE_RESULT_FAIL;
		}
	}
	if(widget_id == WIDGET_ID_HOUR_E){
		if(num < HOURMAX){
			input_time[1].hour = num;
			gkt_widget_set_focus_by_id(WIDGET_ID_MIN_E);
		}
		else{
			event_data->focus_index = 0;
			gkt_widget_set_data_by_id(WIDGET_ID_HOUR_E,(uint32_t)event_data);
			retval = GUEST_UNLOCK_HANDLE_RESULT_FAIL;
		}
	}
	else if(widget_id == WIDGET_ID_MIN_E){
		if(num < MINMAX){
			input_time[1].minute = num;
			retval = GUEST_UNLOCK_HANDLE_RESULT_COMPLETE;
		}
		else{
			event_data->focus_index = 0;
			gkt_widget_set_data_by_id(WIDGET_ID_MIN_E,(uint32_t)event_data);
			retval = GUEST_UNLOCK_HANDLE_RESULT_FAIL;
		}
	}

	return retval;
}

static void window_guest_unlock_time_setting_set_data(gkt_time_u input_time_s,
																			gkt_time_u input_time_e)
{
	gkt_widget_inputbox_data_s time;
	
	window_guest_unlock_time_setting_dec_to_char(input_time_s.hour,&time);
	gkt_widget_set_data_by_id(WIDGET_ID_HOUR_S,(uint32_t)&time);
	
	window_guest_unlock_time_setting_dec_to_char(input_time_s.minute,&time);
	gkt_widget_set_data_by_id(WIDGET_ID_MIN_S,(uint32_t)&time);
	
	window_guest_unlock_time_setting_dec_to_char(input_time_e.hour,&time);
	gkt_widget_set_data_by_id(WIDGET_ID_HOUR_E,(uint32_t)&time);
	
	window_guest_unlock_time_setting_dec_to_char(input_time_e.minute,&time);
	gkt_widget_set_data_by_id(WIDGET_ID_MIN_E,(uint32_t)&time);
}

uint32_t window_guest_unlock_time_click_handle(gkt_widget_id_t widget_id,
														gkt_time_u *input_date,uint32_t num,
															gkt_widget_inputbox_data_s *event_data)
{
	uint8_t retval;
	uint32_t	temp_time=0,time_start=0,time_end=0;
	retval = window_guest_unlock_time_data_handle(widget_id,input_date,num,event_data);
	if(retval == GUEST_UNLOCK_HANDLE_RESULT_COMPLETE){
		time_start = ((input_date[0].hour)<<8) |  (input_date[0].minute);
		time_end = ((input_date[1].hour)<<8) |  (input_date[1].minute);
		if(time_start < time_end){
			temp_time |= (input_date[0].hour)<<24;
			temp_time |= (input_date[0].minute)<<16;
			temp_time |= (input_date[1].hour)<<8;
			temp_time |= (input_date[1].minute)<<0;
		}
	}
	return temp_time;
}

//event handler
static int window_guest_unlock_time_setting_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t widget_id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;
	uint32_t num,temp;

	switch (major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type)
				s_guest_unlock_time_dialog.state = GUEST_UNLOCK_STATE_NORMAL;
			else if(GKT_WINDOW_ACTIVE == minor_type){
				window_common_mask_except_keys(~0);

				if(s_guest_unlock_time_dialog.state == GUEST_UNLOCK_STATE_NORMAL) {
					if (data) {	/* active_1st */
						gkt_window_set_voice(WINDOW_VOICE_COMBINED_ID(please_input_guest_access_time),
							GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR);
					}

					gkt_info("guest_access_time = 0x%08x\n", gp_window_data->guest_access_time);
					u_guest_unlock_time[0].hour = (uint8_t)(gp_window_data->guest_access_time >> 24);
					u_guest_unlock_time[0].minute = (uint8_t)(gp_window_data->guest_access_time >> 16);
					u_guest_unlock_time[1].hour = (uint8_t)(gp_window_data->guest_access_time >> 8);
					u_guest_unlock_time[1].minute = (uint8_t)gp_window_data->guest_access_time;
					window_guest_unlock_time_setting_set_data(u_guest_unlock_time[0],u_guest_unlock_time[1]);
				}
				else{
					gkt_window_exit();
				}
			}
		break;
		case GKT_EVENT_WIDGET:
			if(minor_type == GKT_WIDGET_EVENT_DATA_FULL){
				gkt_widget_inputbox_data_s *event_data;
				event_data = (gkt_widget_inputbox_data_s *)data;
				num = window_guest_unlock_time_setting_to_dec(event_data);
				window_guest_unlock_time_data_handle(widget_id,u_guest_unlock_time,num,event_data);
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			else if(minor_type == GKT_WIDGET_EVENT_CLICK){//
				gkt_widget_inputbox_data_s *event_data;
				event_data = (gkt_widget_inputbox_data_s *)data;
				num = window_guest_unlock_time_setting_to_dec(event_data);
				temp = window_guest_unlock_time_click_handle(widget_id,u_guest_unlock_time,num,event_data);
				if(temp != 0){
					if(temp != gp_window_data->guest_access_time){
						gp_window_data->guest_access_time = temp;
						window_data_save();
					}
					s_guest_unlock_time_dialog.state = GUEST_UNLOCK_STATE_DIALOG_RETURN;

					window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
							WINDOW_TEXT_ID_guest_unlock_time, WINDOW_TEXT_ID_set_success,
							WINDOW_VOICE_COMBINED_ID(set_success));

					gkt_window_exit();
				}
			}
			else if(minor_type == GKT_WIDGET_EVENT_HEAD){
				if(widget_id == WIDGET_ID_MIN_S){
					gkt_widget_set_focus_by_id(WIDGET_ID_HOUR_S);
					retval = GKT_EVENT_HANDLE_STAGE;
				}
				else if(widget_id == WIDGET_ID_HOUR_E){
					gkt_widget_set_focus_by_id(WIDGET_ID_MIN_S);
					retval = GKT_EVENT_HANDLE_STAGE;
				}				
				else if(widget_id == WIDGET_ID_MIN_E){
					gkt_widget_set_focus_by_id(WIDGET_ID_HOUR_E);
					retval = GKT_EVENT_HANDLE_STAGE;
				}
				else{
					gkt_window_exit();
				}
			}
		break;
		case	GKT_EVENT_KEY:
			{
				uint32_t key_value;
				key_value = minor_type;
				if(key_value == GKT_KEY_CANCEL){
					gkt_window_exit();
				}
			}
		break;	
		default:
			break;
	}
	
	return retval;
}
GKT_WINDOW_DESC_DEFINE(guest_unlock_time_setting);

#endif

